`
https://leetcode.cn/problems/maximize-score-of-numbers-in-ranges/
`

/**
 * @param {number[]} start
 * @param {number} d
 * @return {number}
 */
var maxPossibleScore = function (start, d) {
  const n = start.length
  start.sort((a, b) => a - b)

  // 给定 score ，能否从每个区间各选一个数，使得任意两数之差的最小值 >= score
  const check = (score) => {
    let x = -Infinity
    for (const s of start) {
      // 计算下一个数的位置
      x = Math.max(x + score, s)
      if (x > s + d) {
        // 下个数在下个区间的右端点右边，选不了
        return false
      }
    }
    return true
  }

  // 每个区间长度 + 1 不可能是最小绝对差
  let left = 0, right = Math.floor((start[n - 1] - start[0] + d) / (n - 1)) + 1
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      left = mid
    } else {
      right = mid
    }
  }
  return left
};